package jdbc;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCDemo3 {
    public static void main(String[] args) {
        try(Connection connection = DBUtil.getConnection()) {//Connection 可以利用自动关闭特性，使用完毕后关闭它
            Statement statement = connection.createStatement();
            String sql1 = "SELECT id,name,salary,age FROM teacher ;";
            ResultSet rs = statement.executeQuery(sql1);
            while (rs.next()){
                int id = rs.getInt("id");
                String name = rs.getString("name");
                int salary = rs.getInt("salary");
                int age = rs.getInt("age");
//                int id = rs.getInt(1);  //也可以使用指定字段下边，数据库下标从1开始，但不推荐，坑太多
//                String name = rs.getString(2);
//                int salary = rs.getInt(3);
//                int age = rs.getInt(4);
                System.out.println(id+",\t"+name+",\t"+salary+",\t"+age+",\t");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
